package com.miku.blog.properties;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author shkstart
 * @create ${}YEAR-05-29 17:30
 */
@ConfigurationProperties("qiniu-oss")
@Component
@Data
public class QiNiuProperties {

    private String bucket;
    private String accessKey;
    private String secretKey;
    private String url;



    private Configuration configuration;
    private UploadManager uploadManager;
    private BucketManager bucketManager;
    private Auth auth;


    public Configuration getConfiguration(){
        if (Objects.isNull(configuration)){
            configuration = new Configuration(Region.huanan());
        }
        return configuration;
    }

    public UploadManager getUploadManager(){
        if (Objects.isNull(uploadManager)){
            uploadManager = new UploadManager(getConfiguration());
        }
        return uploadManager;
    }

    public BucketManager getBucketManager(){
        if (Objects.isNull(bucketManager)){
            bucketManager = new BucketManager(getAuth(),getConfiguration());
        }
        return bucketManager;
    }

    public Auth getAuth(){
        if (Objects.isNull(auth)){
            auth = Auth.create(this.getAccessKey(),this.getSecretKey());
        }
        return auth;
    }

    public String getUploadToken(){
        return getAuth().uploadToken(this.getBucket());
    }

}
